﻿using System;
using System.Web;

namespace JYH.Framework
{
    /// <summary>
    /// Cookie帮助类
    /// </summary>
    public class CookieHelper
    {
        #region 登录Cookie操作

        private const string _LOGIN_CookieName = "MES_USER_LOGIN_CookieName";

        /// <summary>
        /// 写入cookie值
        /// </summary>
        /// <param name="cookieValue"></param>
        public static void WriteCookie(string cookieValue)
        {
            HttpCookie cookie = new HttpCookie(_LOGIN_CookieName);    //定义cookie对象
            DateTime dt = DateTime.Now;
            TimeSpan span = new TimeSpan(Int16.MaxValue, 0, 0, 0);
            cookie.Expires = dt.Add(span);
            cookie.Value = cookieValue;
            HttpContext.Current.Response.AppendCookie(cookie); //确定写入cookie中
        }

        /// <summary>
        /// 读取cookie值
        /// </summary>
        /// <returns></returns>
        public static string ReadCookie()
        {
            if (HttpContext.Current.Request.Cookies[_LOGIN_CookieName] != null)
            {
                return HttpContext.Current.Request.Cookies[_LOGIN_CookieName].Value;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 重置cookie
        /// </summary>
        /// <param name="cookieValue"></param>
        public static void ResetCookie(string cookieValue)
        {
            RemoveCookie();
            WriteCookie(cookieValue);
        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        public static void RemoveCookie()
        {
            HttpCookie acookie = HttpContext.Current.Request.Cookies[_LOGIN_CookieName];
            if (acookie == null) return;
            acookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(acookie);
        }
        #endregion

        #region Cookie一般操作

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = strValue;
            HttpContext.Current.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        public static void WriteCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string ReadCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return HttpContext.Current.Request.Cookies[strName].Value.ToString();
            }
            return "";
        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        public static void RemoveCookie(string strName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null) return;
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

    }
}
